package com.example.pet.service;

import com.example.pet.dao.PetShopDao;
import com.hj.pet.api.PetShowService;
import com.hj.pet.constant.Code;
import com.hj.pet.model.PetInfo;
import com.hj.pet.model.QueryCondition;
import com.hj.pet.model.ReturnT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PetShopServiceImp implements PetShowService{
    @Autowired
    PetShopDao petShopDao;
    @Override
    public int save(PetInfo petInfo){
        System.out.println(petInfo);
        return petShopDao.save(petInfo);
    }

    @Override
    public int updateById(PetInfo petInfo){
        System.out.println(petInfo);
        return petShopDao.updateById(petInfo);
    }

    @Override
    public PetInfo findPetById(int petId){
        return petShopDao.findPetById(petId);
    }

    @Override
    public List<PetInfo> findPetsByType(String type){
        return petShopDao.findPetsByType(type);
    }

    @Override
    public List<PetInfo> findPetsByPrice(int price1, int price2){
        return petShopDao.findPetsByPrice(price1, price2);
    }

    @Override
    public List<PetInfo> findPetsByUserAccount(String user_account){
        return petShopDao.findPetsByUserAccount(user_account);
    }

    @Override
    public List<PetInfo> findPetsByPetName(String pet_name){
        return petShopDao.findPetsByPetName(pet_name);
    }

    @Override
    public int delPetById(int petId){
        return petShopDao.delPetById(petId);
    }

    @Override
    public List<PetInfo> searchByQuery(QueryCondition condition) {
        return petShopDao.findPetByQuery(condition);
    }

    @Override
    public List<PetInfo> findAll() {
        return petShopDao.findAllPets();
    }

    @Override
    public ReturnT<?> addPet(PetInfo petInfo){
        //检查user_account必须在user_info中可以找到
        if (0 == petShopDao.searchByAccount(petInfo.getUser_account())){
            ReturnT returnT= ReturnT.errorReturn;
            returnT.setMsg("该用户不存在");
            return returnT;
        }
        if (0<petShopDao.save(petInfo)){
            return new ReturnT<>(Code.SUCCESS, "申请成功，等待验证");
        } else {
            return ReturnT.errorReturn;
        }
    }
}
